<?php
include('common/constants.php');
include('common/db.php');

if (!isset( $_GET )){
	echo "Nothing to save.";
}

//create array to temporarily grab variables
$input_arr = array();
//grabs the $_GET variables and adds slashes
foreach ($_GET as $key => $input_arr) {
	$_GET[$key] = addslashes(htmlspecialchars($input_arr));
}

if (isset( $_GET['activation_code'] )){ // SAVE
	$sql = " SELECT activation_code, email, name from `user` ";
	$sql .= " WHERE activation_code='{$_GET['activation_code']}'";
	$result = mysql_query($sql) or trigger_error(mysql_error());
	$num = mysql_num_rows($result);	
	//echo $sql;
		if($num>0){
			$sql = " UPDATE `user` SET activation_code = 'OK' WHERE activation_code='{$_GET['activation_code']}'";
			//echo $sql;
			if(mysql_query($sql)){

				$subject = "Welcome to CarPooling.lk ";

				$message = '';
				$message .= 'Hi ' .mysql_result($result, 0, "name"). ", <br />";
				$message .= "Thanks for activating your CarPooling.lk account. You can now log in to our website using your username and password. <br />";

				$to = mysql_result($result,0,"email"); 

				$headers = "From: " . CONTACT_MAIL_FROM . "\r\n";
				$headers .= 'Cc: ' . CONTACT_MAIL_INTERNAL_CC . "\r\n";
				$headers .= 'Bcc: ' . CONTACT_MAIL_INTERNAL_BCC . "\r\n";
				$headers .= "MIME-Version: 1.0\r\n";
				$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

			    if (mail($to, $subject, $message, $headers)) {
			    	//echo 'Your message has been sent.';
				?>
				<h3>Your account is activated. You will be redirected to carpooling.lk login page in few seconds.</h3>
				<script type="text/javascript">window.setTimeout( function(){window.location = "login.php";}, 3000 );</script>
				<?php
			    } else {
			    	//echo 'There was a problem sending the email.';
			    }
			    // DON'T BOTHER CONTINUING TO THE HTML...
			    die();

			}else{
				echo "<h3>Your account activation failed. You will be redirected to carpooling.lk login page in few seconds.</h3>";
				?>
				<script type="text/javascript">window.setTimeout( function(){window.location = "login.php";}, 3000 );</script>
				<?php
			}
		}else{
			echo "<h3>Your account activation failed. You will be redirected to carpooling.lk login page in few seconds.</h3>";
			?>
			<script type="text/javascript">window.setTimeout( function(){window.location = "login.php";}, 3000 );</script>
			<?php
		}
}?>
